package net.sf.rose.business.action.define;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.sf.rose.business.action.Download;
import net.sf.rose.business.bean.base.DownloadFileBean;
import net.sf.rose.business.bean.control.MenuBean;
import net.sf.rose.business.bean.control.judge.JudgeBean;
import net.sf.rose.business.bean.control.judge.JudgeStatement;
import net.sf.rose.business.service.MenuSevice;
import net.sf.rose.exception.SystemErrorCode;
import net.sf.rose.exception.SystemException;
import net.sf.rose.jdbc.UserBean;
import net.sf.rose.jdbc.service.Service;
import net.sf.rose.web.OnlineUserListener;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author fengjian
 * @since 2014/03/06
 */
@Controller
@RequestMapping("define/menu" )
public class MenuController {

	private static String LIST_PAGE =  "define/menu/list";
	private static String EDIT_PAGE = "define/menu/edit";
	private static String PARENT_ID = "parentID";
	private static String JUDGE_NAME_TEMP = "BASE_DEFINE_MENU_JUDGE_TEMP";

	/**
	 * 
	 */
	@RequestMapping("/list.html")
	public ModelAndView list(HttpServletRequest request, Service service) {
		ModelAndView mav = new ModelAndView(LIST_PAGE);
		MenuBean group = MenuSevice.getService().getMenuBean();
		StringBuilder sb = new StringBuilder();
		this.addGroup(sb, group);
		mav.addObject("group", sb.toString());
		return mav;
	}

	private void addGroup(StringBuilder sb, MenuBean group) {
		sb.append("<li><a onclick=\"treeClick(" + group.getId() + ");\" >").append(group.getText()).append("</a>");
		if (group.getChildMenus().size() > 0) {
			sb.append("<ul>");
			for (MenuBean child : group.getChildMenus()) {
				this.addGroup(sb, child);
			}
			sb.append("</ul>");
		}
		sb.append("</li>");
	}

	/**
	 * 
	 */
	@RequestMapping("/add.html")
	public ModelAndView add(HttpServletRequest request, String parent) {
		MenuBean group = MenuSevice.getService().getMenuBean(parent);
		request.getSession().setAttribute(PARENT_ID, group.getId());
		MenuBean bean = new MenuBean();
		bean.setSort(group.getChildMenus().size());
		request.getSession().setAttribute("group", bean);
		request.getSession().setAttribute(JudgeController.JUDGE_NAME_KEY, JUDGE_NAME_TEMP);
		request.getSession().setAttribute(JUDGE_NAME_TEMP, new JudgeBean());
		ModelAndView mav = new ModelAndView(EDIT_PAGE);
		mav.addObject(PARENT_ID, group.getId());
		return mav;
	}

	/**
	 * 
	 */
	@RequestMapping("/edit.html")
	public ModelAndView edit(HttpServletRequest request, String node) {
		MenuBean bean = MenuSevice.getService().getMenuBean(node);
		request.getSession().setAttribute("group", bean);
		MenuBean parent = MenuSevice.getService().getParent(bean);
		request.getSession().setAttribute(PARENT_ID, parent.getId());
		request.getSession().setAttribute(JudgeController.JUDGE_NAME_KEY, JUDGE_NAME_TEMP);
		JudgeBean judge = null;
		if (bean.getJudge() != null) {
			judge = bean.getJudge();
		} else {
			judge = new JudgeBean();
		}
		request.getSession().setAttribute(JUDGE_NAME_TEMP, judge);
		ModelAndView mav = new ModelAndView(EDIT_PAGE);
		mav.addObject("group", bean);
		mav.addObject(PARENT_ID, parent.getId());
		return mav;
	}

	@RequestMapping("/save.html")
	public ModelAndView save(HttpServletRequest request, MenuBean bean, String parentID) {
		Object obj = request.getSession().getAttribute(JUDGE_NAME_TEMP);
		if (obj != null) {
			JudgeBean judge = (JudgeBean) obj;
			bean.setJudge(judge);
		}
		MenuBean parent = MenuSevice.getService().getMenuBean(parentID);
		if (bean.getId() == null || bean.getId().equals("")) {
			parent.addChildMenu(bean);
		} else {
			MenuBean bak = MenuSevice.getService().getMenuBean(bean.getId());
			bak.update(bean);
		}
		ModelAndView mav = new ModelAndView("ajaxDone");
		mav.addObject("statusCode", 200);
		mav.addObject("message", "操作成功！");
		mav.addObject("callbackType", true);
		//mav.addObject("forwardUrl",);
		return mav;
	}

	@RequestMapping("/delete.html")
	public ModelAndView delete(HttpServletRequest request, String node) {
		MenuBean group = MenuSevice.getService().getMenuBean(node);
		MenuBean parent = MenuSevice.getService().getParent(group);
		parent.removeChildMenus(group.getId());
		ModelAndView mav = new ModelAndView("ajaxDone");
		mav.addObject("statusCode", 200);
		mav.addObject("message", "操作成功！");
		return mav;
	}

	@RequestMapping("/saveAll.html")
	public ModelAndView saveAll(HttpServletRequest request) {
		UserBean user = OnlineUserListener.getOnlineUser(request);
		MenuSevice.save(user);
		ModelAndView mav = new ModelAndView("ajaxDone");
		mav.addObject("statusCode", 200);
		mav.addObject("message", "操作成功！");
		return mav;
	}
	
	/**
	 * 
	 */
	@RequestMapping("/saveTemp.html")
	public ModelAndView saveTemp(HttpServletRequest request,MenuBean bean, String parentID) {
		request.getSession().setAttribute("group", bean);
		request.getSession().setAttribute(PARENT_ID, parentID);
		ModelAndView mav = new ModelAndView(EDIT_PAGE);
		mav.addObject("bean", bean);
		return mav;
	}
	
	@RequestMapping("/getJudgeDescriptions.html")
	public ModelAndView getJudgeDescriptions(HttpServletRequest request) {
		String desc = "";
		Object obj = request.getSession().getAttribute(JUDGE_NAME_TEMP);
		if (obj != null) {
			JudgeBean bean = (JudgeBean) obj;
			if (bean.getJudges() != null) {
				StringBuffer buf = new StringBuffer();
				for (JudgeStatement s : bean.getJudges()) {
					buf.append(s.getDescription()).append(';');
				}
				desc = buf.substring(0, buf.length() - 1);
			}
		}
		ModelAndView mav = new ModelAndView("ajaxDone");
		mav.addObject("statusCode", 200);
		mav.addObject("message", desc);
		return mav;
	}

	/**
	 * 提交操作
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping("/input.html")
	public ModelAndView input(HttpServletRequest request) {
		Object obj = request.getSession().getAttribute(Download.TEMP_FILES);
		if (obj == null) {
			throw new SystemException("Session中找不到上传的文件。", SystemErrorCode.PRARAMTER_LOST_ERROR);
		}
		Map<String, DownloadFileBean> map = (Map<String, DownloadFileBean>) obj;
		InputStream in = null;
		try {
			for (DownloadFileBean bean : map.values()) {
				if (bean.getData() != null) {
					in = bean.getData().getInputStream();
				}
			}
		} catch (IOException e) {
			throw new SystemException("上传的文件读取错误 !", SystemErrorCode.PRARAMTER_LOST_ERROR);
		}
		if (in == null) {
			throw new SystemException("没有找到上传的文件 !", SystemErrorCode.PRARAMTER_LOST_ERROR);
		}
		MenuSevice.input(in);
		ModelAndView mav = null;
		mav = new ModelAndView("ajaxDone");
		mav.addObject("statusCode", 200);
		mav.addObject("message", "导入成功！");
		mav.addObject("callbackType", true);
		return mav;
	}

	/**
	 * 提交操作
	 */
	@RequestMapping("/output.html")
	public String output(HttpServletRequest request) {
		File file = MenuSevice.output();
		request.setAttribute(Download.DOWNLOAD_FILE, file);
		request.setAttribute(Download.DOWNLOAD_FILE_NAME, "菜单配置.xml");
		return "forward:/download.html";
	}
}
